//
//  GetFileSize.m
//  CachesInFilePath
//
//  Created by lanou3g on 15/6/5.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "GetFileSize.h"

@implementation GetFileSize
//应该使用GCD来计算文件大小
+(NSString *)getCachesPathWithExtensionPath:(NSString *)extenionPath{
    
    NSString *cachesDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *filePath = [cachesDir stringByAppendingPathComponent:extenionPath];
    

    
    return filePath;
    
}


//计算缓存文的大小 M
+(long long)fileSizeAtPath:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}


//计算缓存文件夹的大小 M
+(float)folderSizeAtPath:(NSString *)folderPath{
    
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath] ) {
        return 0;
    }
    NSEnumerator * childFileEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName = [childFileEnumerator nextObject];
    long long folderSize = 0;
    while (fileName != nil) {

        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
  
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
         fileName = [childFileEnumerator nextObject];
    }
   
    return folderSize/(1024.0*1024);
    
}

//- (void)countSize{
//    // 获取缓存文件夹路径
//    NSArray * pathsArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//    NSString * filePath = [pathsArray objectAtIndex:0];
//    
//    // 枚举出缓存文件夹路径下的子文件夹
//    NSFileManager * manager = [NSFileManager defaultManager];
//    NSEnumerator * childFilesEnumerator = [[manager subpathsAtPath:filePath] objectEnumerator];
//    
//    __block NSString * fileName = [childFilesEnumerator nextObject];
//    NSString *fileAbsolutePath = [filePath stringByAppendingPathComponent:fileName];
//    
//    // 使用 GCD 队列来并发实现计算缓存文件夹中的缓存数据量的大小,不会阻塞主线程
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_async(queue, ^{
//        NSString *string =nil;
//        // 如果文件路径不存在
//        if (![manager fileExistsAtPath:filePath]) {
//           NSString *string = [NSString stringWithFormat:@"当前缓存:(%.2fMB)",0.0];
//            NSLog(@"%@",string);
//        }
//        else
//        {
//            long long folderSize = 0;
//            while (fileName != nil)
//            {
//                if ([manager fileExistsAtPath:filePath])
//                {
//                    folderSize += [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];
//                    NSLog(@"%lld",folderSize);
//                }
//                fileName = [childFilesEnumerator nextObject];
//            }
//            string = [NSString stringWithFormat:@"当前缓存:(%.2fMB)",(folderSize / (1024.0*1024.0)) / 10];
//            [self performSelectorOnMainThread:@selector(didFinish:) withObject:string waitUntilDone:YES];
//        }
//    });
//}

+ (void)showAllFileWithPath:(void (^)(void))block{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = paths.lastObject;
      //读取缓存里面的具体单个文件/或全部文件//
    
    
   // NSArray *array  = [[NSArray alloc]initWithContentsOfFile:cachesDir];

    NSFileManager * fm = [NSFileManager defaultManager];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if ([fm fileExistsAtPath:cachesDir]) {
            NSArray *files = [fm subpathsAtPath:cachesDir];
     
            for (NSString * path in files) {
                NSString * adfdff= [cachesDir stringByAppendingPathComponent:path];
                [fm removeItemAtPath:adfdff error:nil];
  
                
            }
         }
        
         block();
    });

}

@end
